package com.panda.ziwei.dto;

import com.nlf.calendar.Lunar;
import com.panda.ziwei.utils.TrueSunTimeUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.Calendar;
import java.util.Date;

/**
 * 出生时间信息
 */

@Getter
@Setter
public class BirthInfoDto {

    private String birthCity;

    /** 公历出生时间 */
    private GregorianCalendarDto gregorianCalendar;

    /** 真太阳时出生时间 */
    private TrueSunCalendarDto trueSunCalendar;

    /** 农历出生时间 */
    private LunarCalendarDto lunarCalendar;


    public BirthInfoDto() {
        this.gregorianCalendar = new GregorianCalendarDto();
        this.lunarCalendar = new LunarCalendarDto();
        this.trueSunCalendar = new TrueSunCalendarDto();
    }

    public static BirthInfoDto buildByGregorianCalendar(String city, int year, int month, int day, int hour, int minute) {
        BirthInfoDto birthInfoDto = new BirthInfoDto();
        birthInfoDto.setBirthCity(city);
        //初始化公历出生日期
        initGregorianCalendar(birthInfoDto, year, month, day, hour, minute);
        //计算真太阳时出生日期
        computeTrueSunCalendar(birthInfoDto);
        //计算农历出生日期信息
        computeLunarCalendar(birthInfoDto);
        return birthInfoDto;
    }

    /**
     * 通过公历出生日期计算真太阳时出生日期
     * @param birthInfoDto
     */
    private static void computeTrueSunCalendar(BirthInfoDto birthInfoDto) {
        Date trueSunTimeDate = TrueSunTimeUtil.computeTrueSunTime(birthInfoDto.getBirthCity(), birthInfoDto.getGregorianCalendar().getDate());
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(trueSunTimeDate.getTime());

        TrueSunCalendarDto trueSunCalendarDto = birthInfoDto.getTrueSunCalendar();
        trueSunCalendarDto.setDate(trueSunTimeDate);
        trueSunCalendarDto.setRealYear(calendar.get(Calendar.YEAR));
        trueSunCalendarDto.setRealMonth(calendar.get(Calendar.MONTH));
        trueSunCalendarDto.setRealDay(calendar.get(Calendar.DAY_OF_MONTH));
        trueSunCalendarDto.setRealHour(calendar.get(Calendar.HOUR_OF_DAY));
        trueSunCalendarDto.setRealMinute(calendar.get(Calendar.MINUTE));

    }

    /**
     * 通过公历出生日期计算农历出生日期
     * @param birthInfoDto
     */
    private static void computeLunarCalendar(BirthInfoDto birthInfoDto) {

        Lunar lunar = new Lunar(birthInfoDto.getTrueSunCalendar().getDate());

        LunarCalendarDto lunarCalendarDto = birthInfoDto.getLunarCalendar();
        lunarCalendarDto.setYearNum(lunar.getYear());
        lunarCalendarDto.setYearChinese(lunar.getYearInChinese());
        lunarCalendarDto.setYearGan(lunar.getYearGan());
        lunarCalendarDto.setYearGanIndex(lunar.getYearGanIndex());
        lunarCalendarDto.setYearZhi(lunar.getYearZhi());
        lunarCalendarDto.setYearZhiIndex(lunar.getYearZhiIndex());
        lunarCalendarDto.setMonthNum(lunar.getMonth());
        lunarCalendarDto.setMonthChinese(lunar.getMonthInChinese());
        lunarCalendarDto.setMonthGan(lunar.getMonthGan());
        lunarCalendarDto.setMonthGanIndex(lunar.getMonthGanIndex());
        lunarCalendarDto.setMonthZhi(lunar.getMonthZhi());
        lunarCalendarDto.setMonthZhiIndex(lunar.getMonthZhiIndex());
        lunarCalendarDto.setDay(lunar.getDay());
        lunarCalendarDto.setDayChinese(lunar.getDayInChinese());
        lunarCalendarDto.setDayGan(lunar.getDayGan());
        lunarCalendarDto.setDayGanIndex(lunar.getDayGanIndex());
        lunarCalendarDto.setDayZhi(lunar.getDayZhi());
        lunarCalendarDto.setDayZhiIndex(lunar.getDayZhiIndex());
        lunarCalendarDto.setHourGan(lunar.getTimeGan());
        lunarCalendarDto.setHourGanIndex(lunar.getTimeGanIndex());
        lunarCalendarDto.setHourZhi(lunar.getTimeZhi());
        lunarCalendarDto.setHouZhiIndex(lunar.getTimeZhiIndex());
        lunarCalendarDto.setAnimal(lunar.getYearShengXiao());

    }

    private static void initGregorianCalendar(BirthInfoDto birthInfoDto, int year, int month, int day, int hour, int minute) {
        birthInfoDto.getGregorianCalendar().setYear(year);
        birthInfoDto.getGregorianCalendar().setMonth(month);
        birthInfoDto.getGregorianCalendar().setDay(day);
        birthInfoDto.getGregorianCalendar().setHour(hour);
        birthInfoDto.getGregorianCalendar().setMinute(minute);

        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, 0);
        birthInfoDto.getGregorianCalendar().setDate(calendar.getTime());
    }
}
